package vn.lumi.smarthome.load;

import vn.lumi.smarthome.io.database.TBL_Device;
import vn.lumi.smarthome.io.model.Device;
import vn.lumi.smarthome.io.model.DeviceState;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.content.AsyncTaskLoader;

public class LoadDeviceList extends AsyncTaskLoader<Device[]>{
	private static final String TAG="LoadDeviceList";
	private int mFloorID;
	private int mRoomID;
	
	public LoadDeviceList(Context context,int floor_id,int room_id) {
		super(context);
		mFloorID=floor_id;
		mRoomID=room_id;
	}

	@Override
	public Device[] loadInBackground() {
		Device[] devices=null;
		
		String selection=TBL_Device.ROOM_ID+"=?";
		String[] selectionArgs=new String[]{mRoomID+""};
		String[] projections=new String[]{TBL_Device._ID,TBL_Device.NAME,TBL_Device.TYPE_ID};
		
		Cursor cursor=getContext().getContentResolver().query(TBL_Device.CONTENT_URI_DEVICES, projections, selection, selectionArgs, null);
		if(cursor==null) return null;
		devices=new Device[cursor.getCount()];
		int i=0;
		for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
			String name=cursor.getString(cursor.getColumnIndex(TBL_Device.NAME));
			int device_id=cursor.getInt(cursor.getColumnIndex(TBL_Device._ID));
			int type_id=cursor.getInt(cursor.getColumnIndex(TBL_Device.TYPE_ID));
			
			Device device=new Device(device_id, type_id, mRoomID, name, 0, new DeviceState(), false);
			devices[i]=device;
			i++;
		}
		return devices;
	}

}
